- Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathLCS3Strings.java
117 lines (91 loc) Β· 3.5 KB
/
LCS3Strings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
packagesection19_DynamicProgramming;
publicclassLCS3Strings {
publicstaticvoidmain(String[] args) {
// String s1 = "abcgd", s2 = "bcgad", s3 = "ad"; // 2
Strings1 = "abcd", s2 = "agcfd", s3 = "ad"; // 2
// String s1 = "geeks", s2 = "geeksfor", s3 = "geeksforgeeks"; // 5
// String s1 = "abc", s2 = "agcfd", s3 = "aeg"; // 1
// String s1 = "abcausdhbvaauhdpqweasdgd", s2 = "qwaeohfdasasdfqpaew", s3 = "bnprsdhfasoihqd"; // 2
// String s1 = "abeti", s2 = "btje", s3 = "tjjabt";
System.out.println(lcs3StrRecursion(s1, s2, s3, 0, 0, 0));
// top down
int[][][] storage = newint[s1.length()][s2.length()][s3.length()];
// proper initializing storage
for (inti = 0; i < storage.length; i++) {
for (intj = 0; j < storage[i].length; j++) {
for (intk = 0; k < storage[i][j].length; k++) {
storage[i][j][k] = -1;
}
}
}
System.out.println(lcs3StrTopDownDP(s1, s2, s3, 0, 0, 0, storage));
// bottom up
System.out.println(lcs3StrBottomUpDP(s1, s2, s3));
}
publicstaticintlcs3StrRecursion(Strings1, Strings2, Strings3, intvidx1, intvidx2, intvidx3) {
if (vidx1 == s1.length() || vidx2 == s2.length() || vidx3 == s3.length()) {
return0;
}
charch1 = s1.charAt(vidx1);
charch2 = s2.charAt(vidx2);
charch3 = s3.charAt(vidx3);
intlcsLen = 0;
if (ch1 == ch2 && ch2 == ch3) {
lcsLen = 1 + lcs3StrRecursion(s1, s2, s3, vidx1 + 1, vidx2 + 1, vidx3 + 1);
} else {
intoption1 = lcs3StrRecursion(s1, s2, s3, vidx1 + 1, vidx2, vidx3);
intoption2 = lcs3StrRecursion(s1, s2, s3, vidx1, vidx2 + 1, vidx3);
intoption3 = lcs3StrRecursion(s1, s2, s3, vidx1, vidx2, vidx3 + 1);
lcsLen = Math.max(option1, Math.max(option2, option3));
}
returnlcsLen;
}
publicstaticintlcs3StrTopDownDP(Strings1, Strings2, Strings3, intvidx1, intvidx2, intvidx3,
int[][][] storage) {
if (vidx1 == s1.length() || vidx2 == s2.length() || vidx3 == s3.length()) {
return0;
}
if (storage[vidx1][vidx2][vidx3] != -1) {
returnstorage[vidx1][vidx2][vidx3];
}
charch1 = s1.charAt(vidx1);
charch2 = s2.charAt(vidx2);
charch3 = s3.charAt(vidx3);
intlcsLen = 0;
if (ch1 == ch2 && ch2 == ch3) {
lcsLen = 1 + lcs3StrTopDownDP(s1, s2, s3, vidx1 + 1, vidx2 + 1, vidx3 + 1, storage);
} else {
intoption1 = lcs3StrTopDownDP(s1, s2, s3, vidx1 + 1, vidx2, vidx3, storage);
intoption2 = lcs3StrTopDownDP(s1, s2, s3, vidx1, vidx2 + 1, vidx3, storage);
intoption3 = lcs3StrTopDownDP(s1, s2, s3, vidx1, vidx2, vidx3 + 1, storage);
lcsLen = Math.max(option1, Math.max(option2, option3));
}
storage[vidx1][vidx2][vidx3] = lcsLen;
returnlcsLen;
}
publicstaticintlcs3StrBottomUpDP(Strings1, Strings2, Strings3) {
intone = s1.length(), two = s2.length(), three = s3.length();
int[][][] storage = newint[one + 1][two + 1][three + 1];
for (intblock = one - 1; block >= 0; block--) {
for (introw = two - 1; row >= 0; row--) {
for (intcol = three - 1; col >= 0; col--) {
// logic from top down
charch1 = s1.charAt(block);
charch2 = s2.charAt(row);
charch3 = s3.charAt(col);
intlcsLen = 0;
if (ch1 == ch2 && ch2 == ch3) {
lcsLen = 1 + storage[block + 1][row + 1][col + 1];
} else {
intoption1 = storage[block + 1][row][col];
intoption2 = storage[block][row + 1][col];
intoption3 = storage[block][row][col + 1];
lcsLen = Math.max(option1, Math.max(option2, option3));
}
storage[block][row][col] = lcsLen;
}
}
}
returnstorage[0][0][0];
}
}